package day1.mycomparator;

import java.util.Arrays;
import java.util.Comparator;
import java.util.Random;

/**
 * @author pacai
 * @version 1.0
 */
public class MyComparator {
    public static class Student {
        private int id;
        private String name;
        private int age;

        public Student(int id, String name, int age) {
            this.id = id;
            this.name = name;
            this.age = age;
        }


        public static Student getRandomStudent() {
            Random random = new Random();
            String[] names = new String[]{"张三","李四","王五","赵六","钱期","王八","艾斯盛","奥斯基","傲世"};
            return new Student(random.nextInt(1,100),names[random.nextInt(0,names.length)],random.nextInt(18,70));
        }

        @Override
        public String toString() {
            return "Student{" +
                    "id=" + id +
                    ", name='" + name + '\'' +
                    ", age=" + age +
                    '}';
        }

    }

    public static class IdComparator implements Comparator<Student>{
        //todo 所有的compare方法都有默认机制
        //当 o1 - o2 < 0 时o1排在前
        //当 o1 - o2 > 0 时o2排在前
        //当 o1 - o2 == 0 时谁排在前无所谓
        @Override
        public int compare(Student o1, Student o2) {
            return o1.id - o2.id;
        }
    }




    public static void main(String[] args) {
        int[] arr = new int[]{1, 4, 4, 76, 98, 45, 239};
        Arrays.sort(arr);
        System.out.println(Arrays.toString(arr));

        int stuNum = 10;
        Student[] students = new Student[stuNum];
        for (int i = 0; i < stuNum; i++) {
            students[i] = Student.getRandomStudent();
        }
        System.out.println(Arrays.toString(students));
//        Arrays.sort(students,new MyComparator().new IdComparator());
        Arrays.sort(students,new IdComparator());
        System.out.println(Arrays.toString(students));

    }
}
